# Maintainer: Konstantin Gizdov <arch at kge dot pw>
# Maintainer: Torsten Keßler <tpkessler at archlinux dot org>

pkgname=intel-oneapi-tbb
pkgver=2021.10.0
_debpkgrel=49541
pkgrel=3
pkgdesc="Intel oneAPI Threading Building Blocks"
arch=('loong64' 'x86_64')
url='https://software.intel.com/content/www/us/en/develop/tools/oneapi.html'
license=("custom")
source=("https://apt.repos.intel.com/oneapi/pool/main/${pkgname}-${pkgver}-${pkgver}-${_debpkgrel}_amd64.deb"
        "https://apt.repos.intel.com/oneapi/pool/main/${pkgname}-devel-${pkgver}-${pkgver}-${_debpkgrel}_amd64.deb"
        "https://apt.repos.intel.com/oneapi/pool/main/${pkgname}-common-${pkgver}-${pkgver}-${_debpkgrel}_all.deb"
        "https://apt.repos.intel.com/oneapi/pool/main/${pkgname}-common-devel-${pkgver}-${pkgver}-${_debpkgrel}_all.deb")
b2sums=('cd003a25ace5aee78277611f99f7585e86ddd13639505af657d5396e8787a3a15bb6e7d4587e6dc027dc738882e771249628a92027e72c52619290f2d162e6ea'
        '271c4443a63849962e36a3078b677d00720d2550bae47b260739f02d9dd2995401268e282070dda536766db47902a2128c2a68e780a976692fcbac3a45797646'
        '847a9f007a032890b9fbcdd801d60cb4468b8451d7df524b16c773f4a6d00bbde90d1f3ed4c09e039038acde6d6f589bd92ed4d71a31f856b1c8fc08b5963627'
        '1742a7cc30b777c6ecfbc88b397dd939155f9e25d1a461fdd7865fd46b36b13c8087e10a709149d61677b243f2f99dd295e7ff7d69859f881ef99e63b6b93544')
depends=('intel-oneapi-common')
conflicts=('intel-oneapi-basekit')
noextract=(
    "${pkgname}-${pkgver}-${pkgver}-${_debpkgrel}_amd64.deb"
    "${pkgname}-devel-${pkgver}-${pkgver}-${_debpkgrel}_amd64.deb"
    "${pkgname}-common-${pkgver}-${pkgver}-${_debpkgrel}_all.deb"
    "${pkgname}-common-devel-${pkgver}-${pkgver}-${_debpkgrel}_all.deb"
)

package() {
    cd "${srcdir}"
    ar x "${srcdir}/${pkgname}-${pkgver}-${pkgver}-${_debpkgrel}_amd64.deb"
    tar xvf data.tar.xz -C "${pkgdir}"
    rm data.tar.xz

    ar x "${srcdir}/${pkgname}-devel-${pkgver}-${pkgver}-${_debpkgrel}_amd64.deb"
    tar xvf data.tar.xz -C "${pkgdir}"
    rm data.tar.xz

    ar x "${srcdir}/${pkgname}-common-${pkgver}-${pkgver}-${_debpkgrel}_all.deb"
    tar xvf data.tar.xz -C "${pkgdir}"
    rm data.tar.xz

    ar x "${srcdir}/${pkgname}-common-devel-${pkgver}-${pkgver}-${_debpkgrel}_all.deb"
    tar xvf data.tar.xz -C "${pkgdir}"
    rm data.tar.xz

    # drop conda binaries
    rm -r "${pkgdir}"/opt/intel/oneapi/conda_channel

    # latest symlink
    local _prefix=/opt/intel/oneapi/tbb
    ln -s "${_prefix}/${pkgver}" "${pkgdir}/${_prefix}/latest"

    # fix pkgconfig
    sed "s@prefix=.*@prefix=${_prefix}/latest@g" \
        -i "${pkgdir}/${_prefix}/${pkgver}"/lib/pkgconfig/tbb.pc
    install -d "${pkgdir}"/usr/share/pkgconfig
    ln -s "${_prefix}"/latest/lib/pkgconfig/tbb.pc "${pkgdir}"/usr/share/pkgconfig/tbb.pc

    # cmake
    sed "s@get_filename_component(_tbb_root.*@get_filename_component(_tbb_root ${_prefix}/latest ABSOLUTE)@g" \
        -i "${pkgdir}/${_prefix}/${pkgver}"/lib/cmake/tbb/TBBConfig.cmake
    install -d "${pkgdir}"/usr/share/cmake/TBB
    ln -s "${_prefix}"/latest/lib/cmake/tbb/TBBConfig.cmake "${pkgdir}"/usr/share/cmake/TBB/TBBConfig.cmake
    ln -s "${_prefix}"/latest/lib/cmake/tbb/TBBConfigVersion.cmake "${pkgdir}"/usr/share/cmake/TBB/TBBConfigVersion.cmake

    # ldconfig
    install -d "${pkgdir}"/etc/ld.so.conf.d
    echo "${_prefix}"/latest/"$(sed -n 's/libdir=${prefix}\///p' "${pkgdir}/${_prefix}/${pkgver}"/lib/pkgconfig/tbb.pc)" \
        > "${pkgdir}/etc/ld.so.conf.d/${pkgname}.conf"

    install -d "${pkgdir}"/usr/share/licenses/"${pkgname}"
    ln -s /usr/share/licenses/intel-oneapi "${pkgdir}"/usr/share/licenses/"${pkgname}"/oneapi
    ln -s /opt/intel/oneapi/tbb/"${pkgver}"/licensing "${pkgdir}"/usr/share/licenses/"${pkgname}"/tbb
}
